package com.adminx.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adminx.pojo.MenuPojo;
import com.adminx.service.AccountService;
import com.adminx.util.Constant;

/**
 * 获取菜单
 */
@Controller  
@RequestMapping("menu")  
public class MenuController {
	
	@Autowired
	private AccountService accountService;
	
	/**
	 * 返回json格式菜单
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value = "/jsonMenu", method = RequestMethod.GET)  
	public Map<String, Object> getMenuList(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String menu = this.accountService.getJsonMenu().toString();
			map.put("menu", menu);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 查询权限菜单
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/menuList", method = RequestMethod.GET)  
	public Map<String, Object> getJsonMenu(HttpServletRequest request, HttpServletResponse response){
		HttpSession session = request.getSession();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			@SuppressWarnings("unchecked")
			List<MenuPojo> menu = (List<MenuPojo>)session.getAttribute("menu");
			map.put("menu", menu);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
}
